//include html Js
(function (window, document) {
    var Include = function () {
    }
    Include.prototype = {
        //倒序循环
        forEach: function (array, callback) {
            var size = array.length;
            for (var i = size - 1; i >= 0; i--) {
                callback.apply(array[i], [i]);
            }
        },
        getFilePath: function () {
            var curWwwPath = window.document.location.href;
            var pathName = window.document.location.pathname;
            var localhostPaht = curWwwPath.substring(0, curWwwPath.indexOf(pathName));
            var projectName = pathName.substring(0, pathName.substr(1).lastIndexOf('/') + 1);
            return localhostPaht + projectName;
        },
        //获取文件内容
        getFileContent: function (url) {
            var ie = navigator.userAgent.indexOf('MSIE') > 0;
            var o = ie ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
            o.open('get', url, false);
            o.send(null);
            return o.responseText;
        },
        parseNode: function (content) {
            var objE = document.createElement("div");
            objE.innerHTML = content;
            return objE.childNodes;
        },
        executeScript: function (content) {
            var mac = /<script>([\s\S]*?)<\/script>/g;
            var r = "";
            while (r = mac.exec(content)) {
                eval(r[1]);
            }
        },
        getHtml: function (content) {
            var mac = /<script>([\s\S]*?)<\/script>/g;
            content.replace(mac, "");
            return content;
        },
        getPrevCount: function (src) {
            var mac = /\.\.\//g;
            var count = 0;
            while (mac.exec(src)) {
                count++;
            }
            return count;
        },
        getRequestUrl: function (filePath, src) {
            if (/http:\/\//g.test(src)) {
                return src;
            }
            var prevCount = this.getPrevCount(src);
            while (prevCount--) {
                filePath = filePath.substring(0, filePath.substr(1).lastIndexOf('/') + 1);
            }
            return filePath + "/" + src.replace(/\.\.\//g, "");
        },
        replaceIncludeElements: function () {
            var $this = this;
            var filePath = $this.getFilePath();
            var includeTals = document.getElementsByTagName("include");
            this.forEach(includeTals, function () {
                //拿到路径
                var src = this.getAttribute("src");
                //拿到文件内容
                var content = $this.getFileContent($this.getRequestUrl(filePath, src));
                //将文本转换成节点
                var parent = this.parentNode;
                var includeNodes = $this.parseNode($this.getHtml(content));
                var size = includeNodes.length;
                for (var i = 0; i < size; i++) {
                    parent.insertBefore(includeNodes[0], this);
                }
                //执行文本中的额javascript
                $this.executeScript(content);
                parent.removeChild(this);
                //替换元素 this.parentNode.replaceChild(includeNodes[1], this);
            })
        }
    }
    window.onload = function () {
        new Include().replaceIncludeElements();
        $('.wf-menu.is-menu').on('click',function(event){
            var self=$(event.target.parentNode);
            if(self.hasClass('active')){
                return;
            }else{
                $('.wf-menu.active').removeClass('active');
                self.addClass('active');
            }
        });
        //var wrapper = document.getElementById("wrapper");
        //document.body.removeChild(wrapper);
    }
})(window, document)

var menuClick = function (item) {
    if (item.type == 'link') {
        window.open(item.url);
    } else {
        window.location.href = item.page;
    }
};
var spreadRightNav=function(val){
    if(val){
        $('.right-nav-close').css('left','0%');
        $('.right-nav-bar').css('left','20%');
    }else{
        $('.right-nav-close').css('left','100%');
        $('.right-nav-bar').css('left','100%');
    }
};
function doLogin(){
    var username=$('#loginUser').val();
    var pwd=$('#loginPwd').val();
    if(username==''||username==undefined||pwd==''||pwd==undefined){
        $('#loginFailInfo').text('请输入用户名密码');
        return ;
    }
    console.log(username,pwd);
    $('#userTopCenter .glyphicon').css('display','inline');
    $('#userTopCenter .username').text(username);
    $('#login-div').css('display','none')
//    $('#loginFailInfo').text('用户名或密码错误');
//    $.ajax({
//      type: "POST",
//      url: "test.php",
//      data: {
//        username:username
//        password:pwd
//      },
//      success: function(resp){
//        if(resp.code==0){
//            $('#loginFailInfo').text('');
//          location.href='';
//        }else{
//          $('#loginFailInfo').val(resp.message);
//        }
//      }
//    });
};
var tabArea = function(id){
    var tab = "#tab"+id;
    var tabContent = "#tab-content"+id;
    $(".tab-content").removeClass("active");
    $(".tab").removeClass("active");
    $(tab).addClass("active");
    $(tabContent).addClass("active");
};


$(function() {
    (function($) {
        $.fn.totop = function(opt) {
            var scrolling = false;
            return this.each(function() {
                var $this = $(this);
                $(window).scroll(function() {
                    if (!scrolling) {
                        var sd = $(window).scrollTop();
                        if (sd > 100) {
                            $this.fadeIn();
                        } else {
                            $this.fadeOut();
                        }
                    }
                });

                $this.click(function() {
                    scrolling = true;
                    $('html, body').animate({
                        scrollTop: 0
                    }, 500, function() {
                        scrolling = false;
                        $this.fadeOut();
                    });
                });
            });
        };
    })(jQuery);

    $("#backtotop").totop();

});